<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:ui="http://java.sun.com/jsf/facelets">

<ui:composition template="/template.xhtml">
	<ui:define name="corpo">
		<h:form id="cadForm">
			<h2>Professores</h2>
			<p:growl id="growl" life="2500"></p:growl>


			<p:dialog widgetVar="cadDialog" header="Cadastro Professor"
				closable="false" resizable="false" modal="true" hideEffect="slide"
				showEffect="clip" draggable="false" dynamic="true">
				<h:panelGrid id="panelprof" columns="2" cellpadding="4"
					style="margin:0 auto;">

					<h:outputLabel value="Nome: " />
					<p:inputText id="valnome"
						value="#{professorBean.professor.usuario.nome}" />


					<h:outputLabel value="Matrícula: "></h:outputLabel>
					<p:inputMask id="valmat" mask="99999"
						value="#{professorBean.professor.usuario.matricula}" />

					<p:commandButton value="Grava"
						actionListener="#{professorBean.grava}"
						oncomplete="handleCadastroRequest(xhr, status, args)"
						update="growl panelprof :table:tabprof" />

					<p:commandButton value="Voltar" action="#{professorBean.limpar}"
						onclick="cadDialog.hide()" ajax="true">

						<script type="text/javascript">
							function handleCadastroRequest(xhr, status, args) {
								if (args.validationFailed || !args.gravado) {
									jQuery('cadDialog').effect("shake", {
										times : 3
									}, 100);
								} else {

									cadDialog.hide();
								}
							}
						</script>
					</p:commandButton>
				</h:panelGrid>
			</p:dialog>

		</h:form>

		<h:form id="table">
			<p:dataTable id="tabprof" var="prof"
				value="#{professorBean.professores}" paginator="true" rows="5"
				rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'par' : 'impar'}"
				rowIndexVar="rowIx" emptyMessage="Nenhuma registro encontrado"
				filteredValue="#{professorBean.professoresFiltrados}"
				widgetVar="tableProfessores">
				<f:facet name="header">
					PROFESSORES
					<br></br>
					<br></br>
					<p:outputPanel>
						<h:outputText value="Pesquisar todos os campos " />
						<p:inputText id="globalFilter" onkeyup="tableProfessores.filter()" />
					</p:outputPanel>
				</f:facet>
				<p:columnGroup type="header">
					<p:row>
						<p:column colspan="1" headerText="Nome"
							filterBy="#{prof.usuario.nome}" filterMatchMode="contains"/>
						<p:column colspan="1" headerText="Sobrenome"
							filterBy="#{prof.usuario.sobrenome}" filterMatchMode="contains" />
						<p:column colspan="1" headerText="Matrícula"
							filterBy="#{prof.usuario.matricula}" filterMatchMode="contains"/>
						<p:column colspan="2" headerText="Competência" />
					</p:row>
				</p:columnGroup>

				<p:column>
					#{prof.usuario.nome}
				</p:column>
				<p:column>
					#{prof.usuario.sobrenome}
				</p:column>
				<p:column>
					#{prof.usuario.matricula}
				</p:column>

				

				<p:column>
					<div align="center">
						<p:commandLink
							update=":cadastroComp:cadcomppanel :cadastroComp:tablecomp"
							title="Cadastrar Competência" oncomplete="cadcompdialog.show()"
							action="#{competenciaBean.preencheCompetencias}"
							rendered="#{not empty disciplinaBean.disciplinas}">
							<f:setPropertyActionListener value="#{prof}"
								target="#{competenciaBean.professor}" />
							<p:graphicImage value="resources/imagens/pasta.png" width="24px"
								height="24px" />
						</p:commandLink>
						<p:commandLink title="Cadastrar Competência"
							oncomplete="cadcompdialogSemDisc.show()"
							rendered="#{empty disciplinaBean.disciplinas}">

							<p:graphicImage value="resources/imagens/pasta.png" width="24px"
								height="24px" />
						</p:commandLink>
					</div>
				</p:column>
			</p:dataTable>
		</h:form>

		<h:form id="formSemDisciplina">
			<p:dialog header="Cadastro Competência"
				widgetVar="cadcompdialogSemDisc" resizable="false" id="cadcompidSem"
				hideEffect="slide" showEffect="fade" modal="true" closable="true"
				draggable="false">
				<f:facet name="header">Favor cadastrar ao menos uma disciplina</f:facet>
				<p:commandButton value="Ok" onclick="cadcompdialogSemDisc.hide()"></p:commandButton>
				<f:facet name="footer"></f:facet>
			</p:dialog>

		</h:form>

		<h:form id="profForm">
			<p:dialog header="Alterar Professor" widgetVar="AlteraDialog"
				draggable="false" resizable="false" id="carDlg" hideEffect="slide"
				showEffect="clip" modal="true" closable="false" dynamic="true">

				<h:panelGrid id="display" columns="2" cellpadding="4"
					style="margin:0 auto;">

					<h:outputLabel value="Nome: " />
					<p:inputText value="#{professorBean.professor.usuario.nome}" />


					<h:outputLabel value="Matricula: "></h:outputLabel>
					<p:inputMask mask="99999"
						value="#{professorBean.professor.usuario.matricula}" />


					<p:commandButton value="Alterar"
						actionListener="#{professorBean.atualiza}"
						oncomplete="handleCadastroRequest(xhr, status, args)"
						update=":cadForm:growl :table:tabprof display" />

					<p:commandButton value="Voltar" action="#{professorBean.limpar}"
						onclick="AlteraDialog.hide()" />

					<script type="text/javascript">
						function handleCadastroRequest(xhr, status, args) {
							if (args.validationFailed || !args.gravado) {
								jQuery('cadDialog').effect("shake", {
									times : 3
								}, 100);
							} else {

								AlteraDialog.hide();
							}
						}
					</script>
				</h:panelGrid>
			</p:dialog>
		</h:form>

		<h:form id="profRemove">

			<p:dialog header="Tem certeza?" widgetVar="confirmDialog"
				resizable="false" id="apagDiscip" showEffect="fade"
				hideEffect="explode" closable="false" modal="true" draggable="false">

				<h:panelGrid id="apagaProf" columns="2" cellpadding="4"
					style="margin:0 auto;">

					<p:commandButton value="Sim, remover"
						action="#{usuarioBean.remove}"
						actionListener="#{professorBean.limpar}" update=":table:tabprof"
						onclick="confirmDialog.hide()" />

					<p:commandButton value="Não remover"
						action="#{professorBean.limpar}" onclick="confirmDialog.hide()" />
				</h:panelGrid>
			</p:dialog>
		</h:form>

		<h:form id="cadastroComp">
			<p:dialog header="Cadastro Competência" widgetVar="cadcompdialog"
				resizable="false" id="cadcompid" hideEffect="slide"
				showEffect="clip" modal="true" closable="true" draggable="false">

				<h:panelGrid id="cadcomppanel" columns="3" cellpadding="4"
					style="margin:0 auto;">

					<p:outputPanel>Professor: </p:outputPanel>
					<h:outputText value="#{competenciaBean.professor.usuario.nome}"></h:outputText>

					<br />
					<br />
					<br />
					<p:selectOneMenu value="#{competenciaBean.idDisciplina}">
						<f:selectItems value="#{disciplinaBean.disciplinas}" var="disc"
							itemLabel="#{disc.nome}" itemValue="#{disc.id}" />
					</p:selectOneMenu>

					<p:commandButton action="#{competenciaBean.grava}"
						value="Adicionar" update="tablecomp :cadForm:growl" />

				</h:panelGrid>

				<p:dataTable id="tablecomp" value="#{competenciaBean.competencias}"
					var="comp" paginator="true" rows="5"
					rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'par' : 'impar'}"
					rowIndexVar="rowIx" emptyMessage="nenhuma competência cadastrada">

					<p:column headerText="Materias lecionadas">
                    	#{comp.disciplina.nome} 
                 	</p:column>

					<p:column>
						<p:commandButton value="remover"
							oncomplete="removeCompetencia.show()"
							update=":formApaga:apagaComp :formApaga:tableAulaSelec"
							action="#{aulaBean.preencheAulasRelacionadasComCompetencia}">
							<f:setPropertyActionListener
								target="#{competenciaBean.competencia}" value="#{comp}" />
							<f:setPropertyActionListener target="#{aulaBean.competencia}"
								value="#{comp}" />
						</p:commandButton>
					</p:column>
				</p:dataTable>

			</p:dialog>
		</h:form>
		<h:form id="formApaga">
			<p:dialog widgetVar="removeCompetencia" resizable="false"
				id="apagCompDialog" showEffect="fade" hideEffect="explode"
				closable="false" modal="true">

				<p:dataTable id="tableAulaSelec"
					value="#{aulaBean.aulasRelacionadas}" var="aula"
					rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'par' : 'impar'}"
					rowIndexVar="rowIx" emptyMessage="nenhuma aula relacionada">
					<f:facet name="header">
					As seguintes aulas estão relacionados com essa disciplina. 
					
						</f:facet>

					<p:column headerText="Dia">
                    	#{aula.disponibilidade.dia.dia} 
                 	</p:column>
					<p:column headerText="Turno">
                    	#{aula.disponibilidade.turno} 
                 	</p:column>
					<p:column headerText="Horario">
                    	#{aula.disponibilidade.horario} 
                 	</p:column>
					<f:facet name="footer">Tem certeza que deseja excluir?</f:facet>
				</p:dataTable>


				<h:panelGrid id="apagaComp" columns="2" cellpadding="4"
					style="margin:0 auto;">

					<p:commandButton value="Sim, remover"
						action="#{competenciaBean.remove}"
						update=":cadastroComp:tablecomp"
						onclick="removeCompetencia.hide()" />

					<p:commandButton value="Não remover"
						onclick="removeCompetencia.hide()" />
				</h:panelGrid>
			</p:dialog>
		</h:form>

	</ui:define>
</ui:composition>

</html>